package cn.com.shadowless.xinning.view.activity;

import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.com.shadowless.xinning.R;
import cn.com.shadowless.xinning.model.order.AdniminiStrativeOrderInfo;
import cn.com.shadowless.xinning.model.order.ChildResult;
import cn.com.shadowless.xinning.model.order.CompanyBudgetOrderInfo;
import cn.com.shadowless.xinning.model.order.DepartBudgetOrderInfo;
import cn.com.shadowless.xinning.model.order.TravelOrderInfo;
import cn.com.shadowless.xinning.utils.FragmentUtils;
import cn.com.shadowless.xinning.view.BaseActivity;
import cn.com.shadowless.xinning.view.fragment.AdminiStrativeChildTabFragment;
import cn.com.shadowless.xinning.view.fragment.CompanyChildTabFragment;
import cn.com.shadowless.xinning.view.fragment.DepartChildTabFragment;
import cn.com.shadowless.xinning.view.fragment.TravelChildTabFragment;

/**
 * The type Tab activity.
 *
 * @author sHadowLess
 */
public class TabActivity extends BaseActivity {

    private static final String TR_TRAVEL_DATA_NAME = "出差费";
    private static final String TR_BUDGET_DATA_NAME = "交际费";
    private static final String XB_MAIN_DATA_NAME = "报销主界面";
    private static final String XB_RESOURCE_DATA_NAME = "报销来源数据";
    private static final String XB_IMPORT_DATA_NAME = "导入EXCEL明细";
    private static final String XB_AMOUNT_DATA_NAME = "部门金额汇总";


    /**
     * The Tab back btn.
     */
    @BindView(R.id.tab_back_btn)
    ImageView tabBackBtn;
    /**
     * The Tab title.
     */
    @BindView(R.id.tab_title)
    TextView tabTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onResume() {
        EventBus.getDefault().removeAllStickyEvents();
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Override
    protected int setLayout() {
        return R.layout.activity_tab;
    }

    @Override
    protected void setData() {

    }

    @Override
    public void onBackPressed() {
        finish();
        super.onBackPressed();
    }

    /**
     * Gets chile tab data.
     *
     * @param childResult the child result
     */
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void getChileTabData(ChildResult<?> childResult) {
        String dataName = childResult.getDataName();
        tabTitle.setText(dataName);
        switch (childResult.getBillCode()) {
            case "BC":
                CompanyChildTabFragment companyChildTabFragment = new CompanyChildTabFragment((List<CompanyBudgetOrderInfo.DataBean.BudgetTypeListBean.BudgetNoDetailListBean>) childResult.getData());
                FragmentUtils.replaceFragment(fragmentManager, R.id.tab_fragment, companyChildTabFragment);
                break;
            case "BB":
                DepartChildTabFragment fragment = new DepartChildTabFragment((List<DepartBudgetOrderInfo.DataBean.BudgetTypeListBean.BudgetNoDetailListBean>) childResult.getData());
                FragmentUtils.replaceFragment(fragmentManager, R.id.tab_fragment, fragment);
                break;
            case "TR":
                TravelChildTabFragment<?> travelChildTabFragment = null;
                if (TR_TRAVEL_DATA_NAME.equals(dataName)) {
                    List<TravelOrderInfo.DataBean.ExpenseListBean> expenseListBeans = (List<TravelOrderInfo.DataBean.ExpenseListBean>) childResult.getData();
                    travelChildTabFragment = new TravelChildTabFragment<>(expenseListBeans, dataName);
                } else if (TR_BUDGET_DATA_NAME.equals(dataName)) {
                    List<TravelOrderInfo.DataBean.ComListBean> comListBeans = (List<TravelOrderInfo.DataBean.ComListBean>) childResult.getData();
                    travelChildTabFragment = new TravelChildTabFragment<>(comListBeans, dataName);
                }
                FragmentUtils.replaceFragment(fragmentManager, R.id.tab_fragment, travelChildTabFragment);
                break;
            case "XB":
                AdminiStrativeChildTabFragment<?> adminiStrativeChildTabFragment = null;
                if (XB_MAIN_DATA_NAME.equals(dataName)) {
                    List<AdniminiStrativeOrderInfo.DataBean.ImportItemListBean.MainPageListBean> mainPageListBeans = (List<AdniminiStrativeOrderInfo.DataBean.ImportItemListBean.MainPageListBean>) childResult.getData();
                    adminiStrativeChildTabFragment = new AdminiStrativeChildTabFragment<>(mainPageListBeans, dataName);
                } else if (XB_RESOURCE_DATA_NAME.equals(dataName)) {
                    List<AdniminiStrativeOrderInfo.DataBean.SourceFormListBean> sourceFormListBeans = (List<AdniminiStrativeOrderInfo.DataBean.SourceFormListBean>) childResult.getData();
                    adminiStrativeChildTabFragment = new AdminiStrativeChildTabFragment<>(sourceFormListBeans, dataName);
                } else if (XB_IMPORT_DATA_NAME.equals(dataName)) {
                    List<AdniminiStrativeOrderInfo.DataBean.ImportItemListBean> importItemListBeans = (List<AdniminiStrativeOrderInfo.DataBean.ImportItemListBean>) childResult.getData();
                    adminiStrativeChildTabFragment = new AdminiStrativeChildTabFragment<>(importItemListBeans, dataName);
                } else if (XB_AMOUNT_DATA_NAME.equals(dataName)) {
                    List<AdniminiStrativeOrderInfo.DataBean.ImportItemListBean.AmountTotalListBean> amountTotalListBeans = (List<AdniminiStrativeOrderInfo.DataBean.ImportItemListBean.AmountTotalListBean>) childResult.getData();
                    adminiStrativeChildTabFragment = new AdminiStrativeChildTabFragment<>(amountTotalListBeans, dataName);
                }
                FragmentUtils.replaceFragment(fragmentManager, R.id.tab_fragment, adminiStrativeChildTabFragment);
                break;
            default:
                break;
        }
    }

    @OnClick(R.id.tab_back_btn)
    public void onViewClicked() {
        finish();
    }
}